var shareSome = document.getElementsByClassName("shareList")[0];
var shareAll = document.getElementsByClassName("shareOther")[0];
var smallMask = document.getElementsByClassName("smallMask")[0];
var bigMask = document.getElementsByClassName("bigMask")[0];

var hideSmall = document.getElementsByClassName("smallMask")[0];
hideSmall.onclick = function(){
    shareSome.style.display = "none";
    smallMask.style.display = "none"
}
var hideBig = document.getElementsByClassName("bigMask")[0];
hideBig.onclick = function(){
    shareAll.style.display = "none";
    bigMask.style.display = "none";
}
var shareBtn = document.getElementsByClassName("shareButton")[0];
shareBtn.onclick = function(){
    shareSome.style.display = "block";
    smallMask.style.display = "block";
    gtag('event', 'click', {
        'event_category': 'share',
        'event_label': 'shareButton',
    });
}
var shareMore = document.getElementsByClassName("shareMore")[0];
shareMore.onclick = function(){
    shareSome.style.display = "none";
    shareAll.style.display = "block";
    smallMask.style.display = "none";
    bigMask.style.display = "block";
    gtag('event', 'click', {
        'event_category': 'share',
        'event_label': 'moreButton',
    });
}

function goSocial(type) {
    shareAll.style.display = "none";
    bigMask.style.display = "none";
    gtag('event', 'click', {
        'event_category': 'share',
        'event_label': type,
    });
    let option = init();
    // 判断浏览器是pc端还是移动端
    var mobileAgent = new Array("iphone", "android", "blackberry", "webos", "incognito", "webmate", "bada", "nokia", "lg", "ucweb", "skyfire");
    var browser = navigator.userAgent.toLowerCase();
    var isMobile = false;
    for (var i = 0; i < mobileAgent.length; i++) {
        if (browser.indexOf(mobileAgent[i]) != -1) {
            isMobile = true;
            // console.log(mobileAgent[i]);
            // location.href = '手机要访问页面的链接';
            window.open(social[type](option), '_self')
            break;
        } else {
            isMobile = false;
            // console.log(social[type](option))
            window.open(social[type](option), '', 'toolbar=0,status=0,scrollbars=1,width=626,height=436')
            // console.log('pc浏览器啦！')
        }
    };
    return;
}
function _getURL(options) {
    if (options.url === '') {
        return window.location.href;
    } else {
        return options.url;
    }
}
function init() {
    let title = document.title;
    let url = window.location.href;
    let description = '';
    let metas = document.getElementsByTagName('meta');
    for (let i = 0 ; i < metas.length; i++) {
        if (metas[i].getAttribute('name') === 'description') {
            description = metas[i].getAttribute('content');
            break;
        }
    }
    let image = "";
    let option =  {
        'title': title,
        'url': url,
        'text': description,
        'image': image,
    }
    return option;
}
var social = {
    // default handler
    unknown: function (options) {
        return encodeURIComponent(_getURL(options));
    },

    // vk.com - ВКонтакте
    vk: function (options) {
        return 'http://vk.com/share.php'
            + '?url=' + encodeURIComponent(_getURL(options))
            + '&title=' + encodeURIComponent(options.title)
            + '&description=' + encodeURIComponent(options.text)
            + '&image=' + encodeURIComponent(options.image)
            + '&noparse=true';
    },

    // ok.ru - Одноклассники
    ok: function (options) {
        return 'https://connect.ok.ru/offer'
            + '?url=' + encodeURIComponent(_getURL(options))
            + '&title=' + encodeURIComponent(options.title)
            + '&description=' + encodeURIComponent(options.text)
            + '&imageUrl=' + encodeURIComponent(options.image);
    },

    // Facebook
    fb: function (options) {
        return 'https://www.facebook.com/sharer.php'
            + '?u=' + encodeURIComponent(_getURL(options));
    },

    facebook: function (options) {
        return 'https://www.facebook.com/sharer.php'
            + '?u=' + encodeURIComponent(_getURL(options));
    },

    // Google bookmarks
    googlebookmarks: function (options) {
        return 'https://www.google.com/bookmarks/mark'
            + '?op=edit'
            + '&bkmk=' + encodeURIComponent(_getURL(options))
            + '&title=' + encodeURIComponent(options.title)
            + '&annotation=' + encodeURIComponent(options.text)
            + '&labels=';
    },

    // Livejournal
    livejournal: function (options) {
        return 'http://livejournal.com/update.bml'
            + '?subject=' + encodeURIComponent(options.title)
            + '&event=' + encodeURIComponent(options.text + '<br/><a href="' + _getURL(options) + '">' + options.title + '</a>')
            + '&transform=1';
    },

    // Tumblr
    tumblr: function (options) {
        return 'https://www.tumblr.com/widgets/share/tool'
            + '?canonicalUrl=' + encodeURIComponent(_getURL(options))
            + '&title=' + encodeURIComponent(options.title)
            + '&caption=' + encodeURIComponent(options.text)
            + '&tags=';
    },

    // Pinterest
    pinterest: function (options) {
        return 'http://pinterest.com/pin/create/link/'
            + '?url=' + encodeURIComponent(_getURL(options));
    },

    // linkedin
    linkedin: function (options) {
        return 'https://www.linkedin.com/shareArticle'
            + '?mini=true'
            + '&url=' + encodeURIComponent(_getURL(options))
            + '&title=' + encodeURIComponent(options.title)
            + '&summary=' + encodeURIComponent(options.text);
    },

    // Reddit
    reddit: function (options) {
        return 'https://reddit.com/submit'
            + '?url=' + encodeURIComponent(_getURL(options))
            + '&title=' + encodeURIComponent(options.title);
    },

    // Twitter
    twitter: function (options) {
        var url = _getURL(options);
        return 'http://twitter.com/share'
            + '?text=' + encodeURIComponent(options.title)
            + '&url=' + encodeURIComponent(url)
            + '&counturl=' + encodeURIComponent(url);
    },

    // Mail.ru
    mailru: function (options) {
        return 'http://connect.mail.ru/share'
            + '?url=' + encodeURIComponent(_getURL(options))
            + '&title=' + encodeURIComponent(options.title)
            + '&description=' + encodeURIComponent(options.text)
            + '&imageurl=' + encodeURIComponent(options.image);
    },

    // Google+
    googleplus: function (options) {
        return 'https://plus.google.com/share'
            + '?url=' + encodeURIComponent(_getURL(options));
    },

    // Weibo
    weibo: function (options) {
        return 'http://service.weibo.com/share/share.php'
            + '?url=' + encodeURIComponent(_getURL(options))
            + '&title=' + encodeURIComponent(options.title)
            + '&pic=' + encodeURIComponent(options.image);
    },

    // Telegram
    telegram: function (options) {
        return 'https://telegram.me/share/url'
            + '?url=' + encodeURIComponent(_getURL(options))
            + '&text=' + encodeURIComponent(options.title);
    },

    // WhatsApp
    whatsapp: function (options) {
        return 'https://wa.me/'
            + '?text=' + encodeURIComponent(_getURL(options) + "\n" + options.title);
    },

    // Viber
    viber: function (options) {
        return 'viber://forward'
            + '?text=' + encodeURIComponent(_getURL(options) + "\n" + options.title);
    },

    // Skype
    skype: function (options) {
        return 'https://web.skype.com/share'
            + '?url=' + encodeURIComponent(_getURL(options))
            + '&text=' + encodeURIComponent(options.title);
    },

    // Line.me
    line: function (options) {
        return 'https://lineit.line.me/share/ui'
            + '?url=' + encodeURIComponent(_getURL(options))
            + '&text=' + encodeURIComponent(options.title);
    },

    // E-mail
    email: function (options) {
        return 'mailto:'
            + '?subject=' + encodeURIComponent(options.title)
            + '&body=' + encodeURIComponent(_getURL(options))
            + encodeURIComponent("\n" + options.text);
    }
};
